import Vue from 'vue'
import { login, loginByHid, logout, getInfo } from '@/api/auth'
import { updatePassword } from '@/api/auth'
import { ACCESS_TOKEN, LOGIN_CONFIG } from '@/store/mutation-types'

const user = {
  state: {
    token: '',
    info: {},
    roles: [],
    // 登录页配置
    logoPic: '',
    iconPic: '',
    loginPic: '',
    title: '',
    url: '',
    code: ''
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },

    SET_ROLES: (state, roles) => {
      state.roles = roles
    },

    SET_INFO: (state, info) => {
      state.info = info
    },

    SET_LOGIN_CONFIG: (state, config) => {
      Vue.ls.set(LOGIN_CONFIG, config)
      const { logoPic, loginPic, iconPic, url, code } = config
      state.loginPic = loginPic
      state.logoPic = logoPic
      state.iconPic = iconPic
      // state.title = title;
      state.url = url
      state.code = code
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo)
          .then(response => {
            if (response.code === 'sso.ok') {
              const token = response.data
              Vue.ls.set(ACCESS_TOKEN, token, 7 * 24 * 60 * 60 * 1000)
              commit('SET_TOKEN', token)
              resolve(response.data)
            } else {
              reject(response)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    // hid 登录
    LoginByHid({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        loginByHid(userInfo)
          .then(response => {
            if (response.code === 'sso.ok') {
              const token = response.data
              Vue.ls.set(ACCESS_TOKEN, token, 7 * 24 * 60 * 60 * 1000)
              commit('SET_TOKEN', token)
              resolve(response.data)
            } else {
              reject(response)
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    // 获取用户信息
    GetInfo({ commit }) {
      return new Promise((resolve, reject) => {
        getInfo()
          .then(response => {
            const code = response.code
            const result = response.data
            if (code === 'sso.ok') {
              if (result.role) {
                commit('SET_ROLES', [result.role])
                commit('SET_INFO', result)
              } else {
                reject(new Error('getInfo: roles must be a non-null!'))
              }
            } else {
              reject(new Error('系统错误，请联系管理员！'))
            }
            resolve(result)
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    // 登出
    Logout({ commit }) {
      return new Promise((resolve, reject) => {
        logout()
          .then(response => {
            console.log(response, 'response')
            if (response.code === 'sso.ok') {
              commit('SET_TOKEN', '')
              Vue.ls.remove(ACCESS_TOKEN)
              resolve()
            } else {
              reject()
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    // 修改密码
    UpdatePassword({ commit }, params) {
      return new Promise((resolve, reject) => {
        updatePassword(params)
          .then(response => {
            if (response.code === 'sso.ok') {
              commit('SET_TOKEN', '')
              Vue.ls.remove(ACCESS_TOKEN)
              resolve()
            } else {
              reject()
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    // 更新企业配置信息
    setLoginConfig({ commit }, config) {
      commit('SET_LOGIN_CONFIG', config)
    }
  }
}

export default user
